package com.koron.sinoprobe.web.engineering.service.impl;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.koron.bean.query.PageQuery;
import com.koron.common.core.interceptor.OrderByInterceptor;
import com.koron.sinoprobe.config.Constants;
import com.koron.sinoprobe.web.engineering.bean.po.SpAppcept;
import com.koron.sinoprobe.web.engineering.bean.po.SpEstablish;
import com.koron.sinoprobe.web.engineering.bean.query.SpAppceptQuery;
import com.koron.sinoprobe.web.engineering.bean.vo.SpAppceptVO;
import com.koron.sinoprobe.web.engineering.mapper.SpAppceptMapper;
import com.koron.sinoprobe.web.engineering.service.SpAppceptProjectService;
import com.koron.sinoprobe.web.engineering.service.SpAppceptService;
import com.koron.sinoprobe.web.engineering.service.SpEstablishService;
import com.koron.sinoprobe.web.ledger.bean.entity.SpProject;
import com.koron.sinoprobe.web.ledger.service.SpProjectService;
import org.koron.ebs.mybatis.SessionFactory;
import org.koron.ebs.mybatis.TaskAnnotation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

/**
 * 项目验收(SpAppcept)表服务实现类
 *
 * @author jzp
 * @since 2021-12-02 08:48:54
 */
@Service
public class SpAppceptServiceImpl implements SpAppceptService {

    @Autowired
    private SpAppceptProjectService spAppceptProjectService;

    @Autowired
    private SpEstablishService spEstablishService;

    @Autowired
    private SpProjectService spProjectService;

    /**
     * 通过ID查询单条数据
     */
    @Override
    @TaskAnnotation("getById")
    public SpAppceptVO getById(SessionFactory factory, String id) {
        SpAppceptMapper mapper = factory.getMapper(SpAppceptMapper.class);
        SpAppceptVO spAppceptVO = mapper.selectVoById(id);
        SpEstablish spEstablish = spEstablishService.getByProjectId(factory, spAppceptVO.getProjectId());
        spAppceptVO.setSpEstablish(spEstablish);
        SpProject spProject = spProjectService.getById(factory, spAppceptVO.getProjectId());
        spAppceptVO.setSpProject(spProject);
        return spAppceptVO;
    }

    /**
     * 大于0表示所有项目都走完了完工验收
     */
    @Override
    @TaskAnnotation("countByProjectIdAndType")
    public int countByProjectIdAndType(SessionFactory factory, SpAppcept spAppcept) {
        SpAppceptMapper mapper = factory.getMapper(SpAppceptMapper.class);
        return mapper.countByProjectIdAndType(spAppcept) == mapper.countByProjectIdAndTypeRef(spAppcept) ? 1 : 0;
    }

    /**
     * 查询所有数据
     */
    @Override
    @TaskAnnotation("list")
    public List<SpAppceptVO> list(SessionFactory factory, SpAppceptQuery query) {
        SpAppceptMapper mapper = factory.getMapper(SpAppceptMapper.class);
        return mapper.selectList(query);
    }

    @Override
    @TaskAnnotation("page")
    public PageInfo<SpAppceptVO> page(SessionFactory factory, PageQuery pageQuery, SpAppceptQuery query) {
        OrderByInterceptor.setOrderBy(pageQuery.getOrders());            // 设置排序，若不设置排序，插件会因为没有排序字段而直接跳过，不会影响分页插件
        PageHelper.startPage(pageQuery.getPage(), pageQuery.getPageSize());  //设置分页
        List<SpAppceptVO> spAppceptList = list(factory, query);
        return new PageInfo<>(spAppceptList);
    }

    /**
     * 新增数据
     */
    @Override
    @TaskAnnotation("saveOrUpdate")
    public boolean saveOrUpdate(SessionFactory factory, SpAppcept spAppcept) {
        // 默认草稿
        if (StringUtils.isEmpty(spAppcept.getStatus())) {
            spAppcept.setStatus(Constants.AppceptStatus.DRAFT);
        }
        SpAppceptMapper mapper = factory.getMapper(SpAppceptMapper.class);
        return mapper.insertOrUpdate(spAppcept) > 0;
    }

    @Override
    @TaskAnnotation("saveOrUpdateVo")
    public boolean saveOrUpdateVo(SessionFactory factory, SpAppceptVO spAppceptVO) {
        // 1. 保存主题信息
        this.saveOrUpdate(factory, spAppceptVO);

        if ("6".equals(spAppceptVO.getAcceptType())) {
            // 6竣工验收 不保存项目信息
            // 若项目的某个合同中要求必须做完工验收，则必须等这些合同全部完工验收后，才能进行项目的竣工验收
            /*
             * 关联合同时，只能关联未被关联过的，且”是否要做完工验收“选”是“的合同
             * 所以，竣工验收前
             *  1. 检查改完工验收的项目是否有关联合同（完工验收）
             *  2. 合同中是否标注要进行竣工验收
             *  3. 完工验收验收流程必须走完
             */
            spAppceptVO.setAcceptType("5");
            spAppceptVO.setStatus(Constants.AppceptStatus.APPROVED);
            int i = countByProjectIdAndType(factory, spAppceptVO);
            Assert.state(i > 0, "进行竣工验收前，该项目关联的合同必须要先走完所有的完工验收流程");
            return true;
        }

        // 2. 保存项目信息
        spAppceptProjectService.removeByDivisionId(factory, spAppceptVO.getId());
        spAppceptVO.getProjectList().forEach(e -> e.setAppceptId(spAppceptVO.getId()));
        spAppceptProjectService.saveBatch(factory, spAppceptVO.getProjectList());
        return true;
    }

    @Override
    @TaskAnnotation("end")
    public boolean end(SessionFactory factory, String id) {
        // 1. 更新状态
        SpAppceptVO spAppceptVO = new SpAppceptVO();
        spAppceptVO.setId(id);
        spAppceptVO.setStatus(Constants.AppceptStatus.APPROVED);
        spAppceptVO.setFinishTime(new Date());
        return update(factory, spAppceptVO);
    }

    @Override
    @TaskAnnotation("submit")
    public boolean submit(SessionFactory factory, SpAppcept spAppcept) {
        if ("6".equals(spAppcept.getAcceptType())) {
            // 竣工验收为已完成
            spAppcept.setStatus(Constants.AppceptStatus.FINISHED);
            saveOrUpdate(factory, spAppcept);
            // 2023-08-17 // 竣工验收不走工作流，竣工验收完成后项目状态改为100
            // SpProjectVO spProjectVO = spProjectService.selectOne(factory, spAppcept.getProjectId());
            // spProjectVO.setProgress(100);
            // spProjectService.saveOrUpdate(factory, spProjectVO);
            return true;
        } else {
            spAppcept.setStatus(Constants.AppceptStatus.WAIT_REVIEW);
            saveOrUpdate(factory, spAppcept);
        }

        return true;
    }

    /**
     * 修改数据
     */
    @Override
    @TaskAnnotation("update")
    public boolean update(SessionFactory factory, SpAppcept spAppcept) {
        SpAppceptMapper mapper = factory.getMapper(SpAppceptMapper.class);
        return mapper.update(spAppcept) > 0;
    }

    @Override
    @TaskAnnotation("updateStatus")
    public boolean updateStatus(SessionFactory factory, String id, String status) {
        SpAppcept spAppcept = new SpAppcept();
        spAppcept.setId(id);
        spAppcept.setStatus(status);
        return update(factory, spAppcept);
    }

    /**
     * 通过主键删除数据
     */
    @Override
    @TaskAnnotation("deleteById")
    public boolean deleteById(SessionFactory factory, String id) {
        SpAppcept source = getById(factory, id);
        Assert.state(source.getStatus().equals(Constants.AppceptStatus.DRAFT), "非草稿状态不允许删除");
        SpAppceptMapper mapper = factory.getMapper(SpAppceptMapper.class);
        return mapper.hidden(id) > 0;
    }
}
